package com.huawei.appmarket.service.bundleapp;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Build.VERSION;
import android.os.LocaleList;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoRequest;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoRequest.b;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoResponse;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoResponse.ModuleInfo;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoResponse.SplitApkInfo;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import o.bla;
import o.cdb;
import o.cdg;
import o.cev;
import o.cfw;
import o.cgg;
import o.cvt;
import o.diu;
import o.Logger;
import o.ContentManager;
import o.dvr;
import o.dvt;
import o.CollectCheck;

public class LocaleChangeReceiver extends SafeBroadcastReceiver {
    private static String ॱ = "";

    public void onReceiveMsg(Context context, Intent intent) {
        if ("android.intent.action.LOCALE_CHANGED".equals(intent.getAction())) {
            boolean ˎ = diu.ˊ().ˎ();
            Logger.ˎ("LocaleChangeReceiver", "onReceive ACTION_LOCALE_CHANGED, agreement isAgreed: " + ˎ);
            if (ˎ) {
                String ˊ = ˊ(context);
                if (ॱ.equals(ˊ)) {
                    Logger.ˎ("LocaleChangeReceiver", "same languageSHA256");
                    return;
                }
                ˋ(ˊ);
                Logger.ˎ("LocaleChangeReceiver", "run downloadSplitApks");
                ˏ(context);
            }
        }
    }

    private static void ˋ(String str) {
        ॱ = str;
    }

    private String ˊ(Context context) {
        List<String> ॱ = ॱ(context);
        if (CollectCheck.isEmpty(ॱ)) {
            return "";
        }
        Collections.sort(ॱ);
        StringBuilder stringBuilder = new StringBuilder();
        for (String append : ॱ) {
            stringBuilder.append(append);
        }
        return cev.ˋ(stringBuilder.toString());
    }

    private List<String> ॱ(@NonNull Context context) {
        List<String> arrayList = new ArrayList();
        if (VERSION.SDK_INT >= 24) {
            LocaleList locales = context.getResources().getConfiguration().getLocales();
            for (int i = 0; i < locales.size(); i++) {
                CharSequence language = locales.get(i).getLanguage();
                if (!(TextUtils.isEmpty(language) || arrayList.contains(language))) {
                    arrayList.add(language);
                }
            }
        } else {
            arrayList.add(context.getResources().getConfiguration().locale.getLanguage());
        }
        return arrayList;
    }

    private void ˏ(Context context) {
        List<PackageInfo> ʻ = ((cdg) cdb.ॱ(cdg.class)).ʻ();
        if (CollectCheck.isEmpty(ʻ)) {
            Logger.ˎ("LocaleChangeReceiver", "no installed Apps.");
            return;
        }
        for (PackageInfo packageInfo : ʻ) {
            String str = packageInfo.packageName;
            if (TextUtils.isEmpty(str)) {
                Logger.ˋ("LocaleChangeReceiver", "packageName is empty.");
            } else {
                boolean ˋ = bla.ˋ(context, str);
                if (ˋ) {
                    Logger.ˎ("LocaleChangeReceiver", "packageName=" + str + "   isBundleApp=" + ˋ);
                    ॱ(context, str, packageInfo.versionCode);
                }
            }
        }
    }

    private void ॱ(Context context, final String str, final int i) {
        cvt ॱ = new b(context).ˎ(str).ˊ().ˊ(i).ॱ();
        if (ॱ.ॱ()) {
            Logger.ˎ("LocaleChangeReceiver", "No need to install language packs for: " + str);
        } else {
            cfw.ˎ(ॱ, new cgg<GetApksInfoRequest, GetApksInfoResponse>(this) {
                final /* synthetic */ LocaleChangeReceiver ˎ;

                protected void ˋ(@NonNull GetApksInfoRequest getApksInfoRequest, @NonNull GetApksInfoResponse getApksInfoResponse) {
                    if (getApksInfoResponse.ˏ()) {
                        List<ModuleInfo> ˊ = getApksInfoResponse.ˊ();
                        if (CollectCheck.isEmpty(ˊ)) {
                            Logger.ˋ("LocaleChangeReceiver", "moduleInfos is empty.");
                            return;
                        }
                        SessionDownloadTask sessionDownloadTask = new SessionDownloadTask();
                        sessionDownloadTask.ʼ(str);
                        sessionDownloadTask.ʻ(i);
                        sessionDownloadTask.ˋॱ(10);
                        for (ModuleInfo moduleInfo : ˊ) {
                            List ˊ2 = moduleInfo.ˊ();
                            if (CollectCheck.isEmpty(ˊ2)) {
                                Logger.ˋ("LocaleChangeReceiver", "apkInfos is empty.");
                            } else {
                                this.ˎ.ˏ(moduleInfo, sessionDownloadTask, ˊ2, str);
                            }
                        }
                        if (dvr.ˏ(ContentManager.getInstant().getContext(), true)) {
                            Logger.ˎ("LocaleChangeReceiver", "download directly");
                            dvt.ˏ().ʽ(sessionDownloadTask);
                            return;
                        }
                        dvt.ˏ().ॱ(sessionDownloadTask);
                        Logger.ˎ("LocaleChangeReceiver", "can not download directly");
                        return;
                    }
                    Logger.ˋ("LocaleChangeReceiver", "responseBean.getResponseCode()=" + getApksInfoResponse.getResponseCode() + " responseBean.getRtnCode_()=" + getApksInfoResponse.getRtnCode_());
                }
            });
        }
    }

    private void ˏ(ModuleInfo moduleInfo, SessionDownloadTask sessionDownloadTask, List<SplitApkInfo> list, String str) {
        for (SplitApkInfo splitApkInfo : list) {
            SplitTask splitTask = new SplitTask();
            splitTask.ˋ(splitApkInfo.ˋ());
            splitTask.ˊ(splitApkInfo.ˏ());
            splitTask.ˊॱ(str);
            splitTask.ˋ(splitApkInfo.ˊ());
            splitTask.ॱॱ(moduleInfo.ॱ());
            splitTask.ˎ(splitApkInfo.ˎ());
            splitTask.ᐝ(moduleInfo.ˎ());
            sessionDownloadTask.ˏ(splitTask);
        }
    }
}
